package servlets;

import java.io.IOException;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import entities.RadRecord;
import entities.User;

import queries.DBConnector;
import queries.SearchQueries;
import queries.SecurityQueries;
import queries.UserManagementQueries;
import queries.UserQueries;

/**
 * Servlet implementation class Search
 */
@WebServlet("/Search")
public class Search extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Search() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Map<String, String> messages = new HashMap<String, String>();
		request.setAttribute("messages", messages);		
		DBConnector dbConnect = new DBConnector();
		SecurityQueries security = null;
		
		try {
			HttpSession session = request.getSession(true);
			String username = (String) session.getAttribute("username");

			
			String keywords = (request.getParameter("keywords")).trim();
			DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
			java.util.Date fd = df.parse((request.getParameter("from_date")).trim());
			java.util.Date td = df.parse((request.getParameter("to_date")).trim());
			java.sql.Date fromDate = new java.sql.Date(fd.getTime());
			java.sql.Date toDate = new java.sql.Date(td.getTime());	
			
			security = new SecurityQueries(dbConnect.getConnection());
			SearchQueries sq = new SearchQueries(dbConnect.getConnection(), security);				
			UserManagementQueries umq = new UserManagementQueries(dbConnect.getConnection(), security);
			User currentUser = umq.retrieveUserData(username);
			
			ArrayList<RadRecord> records = sq.search(keywords, fromDate, toDate, currentUser);
			
			request.setAttribute("records", records);

			request.getRequestDispatcher("search.jsp").forward(request, response);				
			
		} catch (ParseException e) {
			messages.put("search_msg", "<span class=\"message error\">Records could not be retrieved.</span>");
			request.getRequestDispatcher("search.jsp").forward(request, response);	
			e.printStackTrace();		
		} catch (ClassNotFoundException e) {
			messages.put("search_msg", "<span class=\"message error\">Records could not be retrieved.</span>");
			request.getRequestDispatcher("search.jsp").forward(request, response);	
			e.printStackTrace();
		} catch (SQLException e) {
			messages.put("search_msg", "<span class=\"message error\">Records could not be retrieved.</span>");
			request.getRequestDispatcher("search.jsp").forward(request, response);	
			e.printStackTrace();
		} catch (InstantiationException e) {
			messages.put("search_msg", "<span class=\"message error\">Records could not be retrieved.</span>");
			request.getRequestDispatcher("search.jsp").forward(request, response);	
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			messages.put("search_msg", "<span class=\"message error\">Records could not be retrieved.</span>");
			request.getRequestDispatcher("search.jsp").forward(request, response);	
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
